import { Injectable, OnModuleInit } from '@nestjs/common';
import { Client, ClientGrpc } from '@nestjs/microservices';
import { Observable } from 'rxjs';
import { grpcClientOptions } from './utils/grpc-client/grpc-client.options';

interface UserService {
  getUser(data: { id: number }): Observable<any>; // 根据你的 .proto 定义的服务方法
}

@Injectable()
export class GrpcService implements OnModuleInit {
  @Client(grpcClientOptions)
  private readonly client: ClientGrpc;

  private userService: UserService;

  onModuleInit() {
    // 在模块初始化时，获取 gRPC 服务的引用
    this.userService = this.client.getService<UserService>('UserService'); // 'UserService' 是 .proto 中定义的服务名称
  }

  getUser(id: number) {
    return this.userService.getUser({ id });
  }
}
